// pages/organization/admin.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rightArrow: "../../../image/mine-menu/right-arrow.png",
    isAdmin: "",//是否为管理员
    joinable:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var org_id = options.org_id;
    var isAdmin = options.isAdmin;
    console.log(org_id);
    wx.setStorageSync('tempFilePaths' + org_id, '');
    // wx.setStorageSync('org_id', org_id);
   
    if (isAdmin=="true") {

      var own_orgs = wx.getStorageSync("own_orgs");
      var org = own_orgs[org_id];
    } else {
      var join_orgs = wx.getStorageSync("join_orgs");
      var org = join_orgs[org_id];
    }
    wx.setStorageSync("org", org);
    wx.setStorageSync("org_id", org_id);

     console.log(org);
    that.setData({
      org: org,
      org_id: org_id,
      isAdmin: isAdmin,
      joinable: org.joinable
    });
    wx.setNavigationBarTitle({
      title: "管理组织"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.member_list_request();
    this.setData({
      org:wx.getStorageSync("org"),
    })
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //跳转 
  //修改组织名称跳转
  fix_org_name_nav: function () {
    if (this.data.isAdmin != "true") {
      return;
    }
    var org_id = this.data.org_id;
    var org_name = this.data.org.name;
    wx.navigateTo({
      url: 'modify/org_name?org_id=' + org_id + "&item_name=组织名称&item=org_name&item_type=text&item_default=" + org_name,
    })
  },
  //修改组织头像跳转
  fix_org_avatar_nav: function () {
    if (this.data.isAdmin != "true") {
      return;
    }
    var org_id = this.data.org_id;
    wx.navigateTo({
      url: 'modify/org_avatar?org_id=' + org_id,
    })
  },

  // 修改组织口令
  fix_org_password_nav: function () {
    if (this.data.isAdmin != "true") {
      return;
    }
    var org_id = this.data.org_id;
    var org_password = this.data.org.password;
    wx.navigateTo({
      url: 'modify/org_name?org_id=' + org_id + "&item_name=组织口令&item=org_password&item_type=text&item_default=" + org_password,
    })
  },
  // 跳转邀请
  invite_nav:function(){
    var org_id = this.data.org_id;
    wx.navigateTo({
      url: 'invite?org_id=' + org_id,
    })
  },
  //跳转组员管理
  kick_member: function () {
    if (this.data.isAdmin != "true") {
      return;
    }
    var org_id = this.data.org_id;
    wx.navigateTo({
      url: 'kick?org_id=' + org_id,
    })
  },
  //修改组织跳转
  edit_navigate: function () {
    if (this.data.isAdmin != "true") {
      return;
    }
    var org_id = this.data.org_id;
    wx.navigateTo({
      url: 'modify?org_id=' + org_id,
    })
  },
  //跳转到申请列表
  nav_join_list: function () {
    if (this.data.isAdmin != "true") {
      return;
    }
    var org_id = this.data.org_id;
    wx.navigateTo({
      url: 'join_list?org_id=' + org_id,
    })
  }
  ,
  intro_navigate: function () {
    var intro = this.data.org.intro;

    wx.navigateTo({
      url: 'intro?intro=' + intro,
    })

  },
  //服务器

  //请求成员列表
  member_list_request: function () {
    var org_id = this.data.org_id;
    var yuyue_session = wx.getStorageSync("yuyue_session");
    var that = this;
    //console.log(org_id);
    wx.request({
      url: app.globalData.default_url,
      data: {
        action: "api.v1.organization.member_list",
        org_id: org_id,
        // yuyue_session: yuyue_session,
      },
      success: function (res) {
        console.log(res);
        if(res.data.op=="fail"){
          wx,wx.showModal({
            title: '获取信息失败',
            content: res.data.reason ,
            showCancel: false,
            success: function(res) {
              if(res.confirm){
                wx.navigateBack({
                  
                })
              }
            },
            fail: function(res) {},
            complete: function(res) {},
          })
        }else{
           that.setData({
            member: res.data.data
          })
        }
       

      },fail:function(res){
        wx.showModal({
          title: '网络错误',
          content: '请检查网络连接',
          showCancel:false,
          success:function(res){
            wx.navigateBack({
              
            })
          }
        })
      }
    })
  },

  //请求修改允许加入
  bindjoinable_request: function (e) {
    var checked
    var joinable = e.detail.value
    console.log(joinable);
    var org_id = this.data.org_id;
    var yuyue_session = wx.getStorageSync("yuyue_session");
    var that = this;
    //console.log(org_id);
    wx.request({
      url: app.globalData.default_url,
      data: {
        action: "api.v1.organization.setJoinable",
        org_id: org_id,
        yuyue_session: yuyue_session,
        joinable: joinable,
      },
      success: function (res) {
        if(res.data.op=="fail"){
          wx.showModal({
            title: '修改失败',
            content: '系统错误，请联系管理员',
            showCancel:false,
            success:function(res){
              if(res.confirm){
                that.setData({
                  joinable: joinable
                })
              }
            }
          })
        }

      }
    })
  },
  //踢人
  kick_member_request: function (e) {
    console.log(e);
    var yuyue_session = e.target.dataset.join_session;
    var org_id = this.data.org_id;
    var that = this;
    wx.showModal({
      title: '请出组织',
      content: '确认将该成员请出组织？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.default_url,
            data: {
              action: "api.v1.organization.kick_member",
              org_id: org_id,
              yuyue_session: yuyue_session,
            },
            success: function (res) {
              if (res.data.op != "fail") {
                wx.showModal({
                  title: '操作成功',
                  content: '成功将该成员请出',
                  showCancel: false,
                  confirmText: '确定',
                  confirmColor: '',
                  success: function (res) {
                    if (res.confirm) {
                      that.onShow();
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              } else {
                wx.showModal({
                  title: "操作失败",
                  content: res.data.reason,
                  confirmText: "重试",
                  success: function (res) {
                    if (res.confirm)
                      that.kick_request(yuyue_session);
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  //解散组织
  disband_request: function () {
    if (this.data.isAdmin != "true") {
      return;
    }
    var that = this;
    var org_id = this.data.org_id;
    var yuyue_session = wx.getStorageSync("yuyue_session");
    console.log("disband_request: yuyue_session" + yuyue_session + "org_id" + org_id);
    wx.showModal({
      title: '解散组织',
      content: '确认解散此组织？此操作不可逆！！！',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.default_url,
            data: {
              action: "api.v1.organization.disband",
              org_id: org_id,
              yuyue_session: yuyue_session,
            },
            success: function (res) {
              console.log(res)
              if (res.data.op != "fail")
                wx.showModal({
                  title: '解散组织',
                  content: '解散成功',
                  showCancel: false,
                  confirmText: '确定',
                  success: function (r) {
                    if (r.confirm)
                      wx.navigateBack({

                      })
                  },
                  fail: function (res) {
                    wx.showModal({
                      title: '解散组织',
                      content: '解散失败',
                      showCancel: false,
                      confirmText: '确定',
                      success: function (r) {
                      }
                    })
                  },
                  complete: function (res) { },
                })
              else {
                wx.showModal({
                  title: '操作失败',
                  content: res.data.reason,
                  confirmText: '重试',
                  success: function (r) {
                    if (r.confirm) {
                      that.disband_request();
                    }
                  },
                  fail: function (res) {
                    wx.showModal({
                      title: '请求失败',
                      content: '网络错误',
                      confirmText: '重试',
                      success: function (r) {
                        if (r.confirm) {
                          that.disband_request();
                        }
                      },
                    })
                  }
                })
              }
            }
          })
        }
      }
    })
  },

  //退出组织
  kick_request: function (yuyue_session) {
    var org_id = this.data.org_id;
    var that = this;
    wx.showModal({
      title: '退出组织',
      content: '确认退出此组织？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.default_url,
            data: {
              action: "api.v1.organization.kick_member",
              org_id: org_id,
              yuyue_session: yuyue_session,
            },
            success: function (res) {
              if (res.data.op != "fail") {
                wx.showModal({
                  title: '操作成功',
                  content: '成功退出组织',
                  showCancel: false,
                  confirmText: '确定',
                  confirmColor: '',
                  success: function (res) {
                    if (res.confirm) {
                      wx.navigateBack({

                      })
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              } else {
                wx.showModal({
                  title: "操作失败",
                  content: res.data.reason,
                  confirmText: "重试",
                  success: function (res) {
                    if (res.confirm)
                      that.kick_request(yuyue_session);
                  }
                })
              }
            }
          })
        }

      }
    })

  }

})